/** @format */

import { onMounted, ref } from 'vue'
import { CACHE_KEY } from '../contants/configCont'
import { useEntity } from './useEntity'

const randomMsg = ref('')
const { content, getData } = useEntity({
  cacheKey: CACHE_KEY.helpMsg,
  path: 'helper.json',
})

let id: any = 0
const clearRandom = () => {
  if (id > 0) clearTimeout(id)
}
const start = () => {
  const list = content.value?.list || []
  const index = Math.floor(Math.random() * list.length)
  randomMsg.value = list[index]

  id = setTimeout(() => {
    start()
  }, 3000)
}

export const useHelper = () => {
  onMounted(() => {
    getData()
  })
  start()
  return {
    randomMsg,
    clearRandom,
  }
}
